package com.pw.study.arithmetic.task

import com.pw.study.arithmetic.entities.Boy

import scala.util.control.Breaks

/**
 * @Desc: TODO
 * @author: future
 * @since: 2022/5/29-17:35
 */
object BoyGame {
  var first: Boy = new Boy(-1)

  def addBoys(num: Int): Unit = {
    if (num < 1) {
      println("num的值不正确。。")
      return
    }
    var cur: Boy = null
    for (no <- 1 to num) {
      val boy = new Boy(no)
      if (no == 1) {
        first = boy
        first.next = first
        cur = first
      } else {
        cur.next = boy
        boy.next = first
        cur = boy
      }
    }


  }

  /**
   * 编写方法countBoy，完成游戏
   *
   * @param starNo
   * @param countNum
   * @param num
   */
  def countBoy(starNo: Int, countNum: Int, num: Int): Unit = {
    if (first.next == null || starNo < 1 || starNo > num) {
      println("参数有误，重新输入!")
      return
    }
    var helper = first
    Breaks.breakable {
      while (true) {
        if (helper.next == first) {
          Breaks.break()
        }
        helper = helper.next
      }
    }
    for (i <- 0 until starNo - 1) {
      first = first.next
      helper = helper.next
    }
    Breaks.breakable {
      while (true) {
        if (helper == first) {
          Breaks.break()
        }
        for (i <- 0 until countNum - 1) {
          first = first.next
          helper = helper.next
        }
        println(s"小孩：${first.no} 出圈。。。")
        first = first.next
        helper.next = first
      }
    }
    println(s"留在最后圈内的人是 小孩编号为${first.no}")
  }

  def showBoy(): Unit = {
    if (first.next == null) {
      println("没有任何小孩")
      return
    }
    var cur = first
    Breaks.breakable {
      while (true) {
        println(s"小孩编号：${cur.no}")
        if (cur.next == first) {
          Breaks.break()
        }
        cur = cur.next
      }
    }

  }

  def main(args: Array[String]): Unit = {
    addBoys(5)
    countBoy(1, 10, 5)
    showBoy()
  }
}
